using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;

namespace Acms.SqlDal
{
    public class FileDal : Base
    {
        public FileDal() { }

        public enum Fields
        {
            Id,
            FileData,
            Name,
            Size,
            FileType,
            ContentType,
            Extension,
            Width,
            Height
        }

        protected override void ReadRecord(Hashtable table, SqlDataReader reader)
        {
            GetReaderValue<int>(Fields.Id, reader, table);
            GetReaderValue<byte[]>(Fields.FileData, reader, table);
            GetReaderValue<string>(Fields.Name, reader, table);
            GetReaderValue<long>(Fields.Size, reader, table);
            GetReaderValue<int>(Fields.FileType, reader, table);
            GetReaderValue<string>(Fields.ContentType, reader, table);
            GetReaderValue<string>(Fields.Extension, reader, table);
            GetReaderValue<int?>(Fields.Width, reader, table);
            GetReaderValue<int?>(Fields.Height, reader, table);
        }

        public Hashtable GetById(int id)
        {
            SqlCommand cmd = CreateCommand("GetById");
            AddInputParameter(cmd, "Id", id, System.Data.SqlDbType.Int);
            return base.ExecuteGetItem(cmd);
        }

        public void Update(ref int id, byte[] fileData, string name, long size, int fileType, string contentType, string extension, int? width, int? height, ref object concurencyId)
        {
            SqlCommand cmd = CreateCommand("Put");
            AddInputParameter(cmd, "Id", id, System.Data.SqlDbType.Int);
            AddInputParameter(cmd, "FileData", fileData, System.Data.SqlDbType.Image);
            AddInputParameter(cmd, "Name", name, System.Data.SqlDbType.VarChar, 50);
            AddInputParameter(cmd, "Size", size, System.Data.SqlDbType.BigInt);
            AddInputParameter(cmd, "FileType", fileType, System.Data.SqlDbType.Int);
            AddInputParameter(cmd, "ContentType", contentType, System.Data.SqlDbType.VarChar, 50);
            AddInputParameter(cmd, "Extension", extension, System.Data.SqlDbType.VarChar, 5);
            AddInputParameter(cmd, "Height", height, System.Data.SqlDbType.Int);
            AddInputParameter(cmd, "Width", width, System.Data.SqlDbType.Int);
            AddInputParameter(cmd, "VersionId", concurencyId, System.Data.SqlDbType.Timestamp);
            AddOutputParameter(cmd, "IdOut", System.Data.SqlDbType.Int);
            AddOutputParameter(cmd, "VersionIdOut", System.Data.SqlDbType.Timestamp);

            ExecuteCommand(cmd);

            id = GetOutputValue<int>(cmd, "IdOut");
            concurencyId = GetOutputValue<byte[]>(cmd, "VersionIdOut");
        }

        public void Delete(int id, object concurencyId)
        {
            SqlCommand cmd = CreateCommand("Delete");
            AddInputParameter(cmd, "Id", id, System.Data.SqlDbType.Int);
            AddInputParameter(cmd, "VersionId", concurencyId, System.Data.SqlDbType.Timestamp);
            ExecuteCommand(cmd);
        }

    }
}
